package com.yunfeisoft.business.controller;

import com.yunfeisoft.base.model.User;
import com.yunfeisoft.business.model.Elder;
import com.yunfeisoft.business.model.ElderApprovalResult;
import com.yunfeisoft.business.service.inter.ElderApprovalResultService;
import com.yunfeisoft.business.service.inter.ElderService;
import com.yunfeisoft.common.base.BaseController;
import com.yunfeisoft.common.utils.Page;
import com.yunfeisoft.common.utils.Response;
import com.yunfeisoft.common.utils.ResponseUtils;
import com.yunfeisoft.common.utils.Validator;
import com.yunfeisoft.utils.ApiUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.params.Params;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: r.cheng
 * @Date: 2020/4/1 10:56
 * @Description: 老人入院审批结果controller
 * @Version: 1.0
 */
@RestController
public class ElderApprovalResultController extends BaseController {

    @Autowired
    private ElderApprovalResultService elderApprovalResultService;

    @Autowired
    private ElderService elderService;

    /**
     * 医康部审批
     * @param request
     * @return
     */
    @PostMapping("/web/elderApprovalResult/doctorApproval")
    public Response doctorSave(ElderApprovalResult approvalResult, HttpServletRequest request) {
        // 验证参数
        Validator validator = new Validator();
        validator.required(request, "elderId", "参数错误!");
        validator.required(request, "status", "审批状态为空!");
        validator.required(request, "content", "审批内容为空!");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }
        // 获取审批关联的图片信息
        String[] attachmentIds = ServletRequestUtils.getStringParameters(request, "attachmentIds");
        // 添加审批记录
        elderApprovalResultService.doctorSave(approvalResult, attachmentIds);
        return ResponseUtils.success("医康部审批成功!");
    }

    /**
     * 护理部审批
     * @param request
     * @return
     */
    @PostMapping("/web/elderApprovalResult/nurseApproval")
    public Response nurseSave(HttpServletRequest request) {
        // 验证参数
        Validator validator = new Validator();
        validator.required(request, "elderId", "参数错误!");
        validator.required(request, "status", "审批状态为空!");
        validator.required(request, "content", "审批内容为空!");
        validator.required(request, "nursingLevel", "护理等级为空!");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }
        String nursingLevel = ServletRequestUtils.getStringParameter(request, "nursingLevel", null);
        String elderId = ServletRequestUtils.getStringParameter(request, "elderId", null);
        String status = ServletRequestUtils.getStringParameter(request, "status", null);
        String content = ServletRequestUtils.getStringParameter(request, "content", null);
        Map<String, Object> params = new HashMap<>();
        initParams(params, request);
        params.put("nursingLevel", nursingLevel);
        params.put("elderId", elderId);
        params.put("status", Integer.valueOf(status));
        params.put("content", content);
        // 获取审批关联的图片信息
        String[] attachmentIds = ServletRequestUtils.getStringParameters(request, "attachmentIds");
        // 添加审批记录
        elderApprovalResultService.nurseSave(params, attachmentIds);
        return ResponseUtils.success("护理部审批成功!");
    }

    /**
     * 院办审批
     * @param request
     * @return
     */
    @PostMapping("/web/elderApprovalResult/officeApproval")
    public Response officeSave(ElderApprovalResult approvalResult, HttpServletRequest request) {
        // 验证参数
        Validator validator = new Validator();
        validator.required(request, "elderId", "参数错误!");
        validator.required(request, "status", "审批状态为空!");
        validator.required(request, "content", "审批内容为空!");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }
        // 获取审批关联的图片信息
        String[] attachmentIds = ServletRequestUtils.getStringParameters(request, "attachmentIds");
        // 添加审批记录
        elderApprovalResultService.officeSave(approvalResult, attachmentIds);
        return ResponseUtils.success("院办审批成功!");
    }

    /**
     * 我的审批列表
     * @param request
     * @return
     */
    @PostMapping("/web/elderApprovalResult/my_list")
    public Response myApprovalList(HttpServletRequest request) {
        // 获取参数
        String name = ServletRequestUtils.getStringParameter(request, "name", null);
        int nursingLevel = ServletRequestUtils.getIntParameter(request, "nursingLevel", -1);
        int status = ServletRequestUtils.getIntParameter(request, "status", -1);
        // 获取用户信息
        User user = ApiUtils.getLoginUser();
        Map<String, Object> params = new HashMap<>();
        initParams(params, request);
        // 保存参数
        params.put("name", name);
        params.put("createId", user.getId());
        if (status > 0) {
            params.put("status", status);
        }
        if (nursingLevel > 0) {
            params.put("nursingLevel", nursingLevel);
        }
        // 查询 我的审批列表
        Page<Elder> page = elderService.myApprovalList(params);
        return ResponseUtils.success(page);
    }

}
